package com.elinkway.infinitemovies.view;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Looper;
import android.os.MessageQueue.IdleHandler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;
import com.lvideo.component.extraplayer.utils.SohuPlayerDefinition;

import java.util.List;
import java.util.Map;


public class ClarityPopupWindow {
    private PopupWindow mSiftPopupWindow;
    private View mPopWindowView;
    private Context context;
    private View mparentView;

    private TextView mHighclear;// 高清
    private TextView mCommon;// 标清
    private TextView mFluency;// 流畅
    private TextView mSuper;//超清

    private OnClickListener mClickListener;

    public ClarityPopupWindow(Context context, View parentView, OnClickListener clickListener) {
        this.context = context;
        mparentView = parentView;
        mClickListener = clickListener;
        intPopupWindow();
    }

    public void intPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(context);

        // 加清晰度弹出的Popwind模板
        mPopWindowView = inflater.inflate(R.layout.claritypop, null);
        mSiftPopupWindow =
                new PopupWindow(mPopWindowView, LayoutParams.WRAP_CONTENT,
                        LayoutParams.MATCH_PARENT);

        if (null != mSiftPopupWindow) {
            mSiftPopupWindow.setFocusable(false);
            // 用于POPwindow点击消失
            mSiftPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mSiftPopupWindow.setOutsideTouchable(true);
            mSiftPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                }
            });
        }
        initView();
    }

    public void showOutsitePopView() {
        mSiftPopupWindow.showAtLocation(mparentView, Gravity.RIGHT, 0, 0);
    }

    public void initOutSiteClarityUI(List<String> clarityList, String crrentClarity) {
        mSuper.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mHighclear.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mCommon.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mFluency.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        if (clarityList.contains(MoviesConstant.SUPER2)) {
            mSuper.setVisibility(View.VISIBLE);
            mSuper.setClickable(true);
        } else {
            mSuper.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mSuper.setClickable(false);
        }
        if (clarityList.contains(MoviesConstant.SUPER)) {
            mHighclear.setVisibility(View.VISIBLE);
            mHighclear.setClickable(true);
        } else {
            mHighclear.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mHighclear.setClickable(false);
        }
        if (clarityList.contains(MoviesConstant.HIGH)) {
            mCommon.setVisibility(View.VISIBLE);
            mCommon.setClickable(true);
        } else {
            mCommon.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mCommon.setClickable(false);
        }
        if (clarityList.contains(MoviesConstant.NORMAL)) {
            mFluency.setVisibility(View.VISIBLE);
            mFluency.setClickable(true);
        } else {
            mFluency.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mFluency.setClickable(false);
        }

        if (MoviesConstant.SUPER2.equalsIgnoreCase(crrentClarity)) {
            mSuper.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (MoviesConstant.SUPER.equalsIgnoreCase(crrentClarity)) {
            mHighclear.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (MoviesConstant.HIGH.equalsIgnoreCase(crrentClarity)) {
            mCommon.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (MoviesConstant.NORMAL.equalsIgnoreCase(crrentClarity)) {
            mFluency.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        }
    }

    public void initSohuSDKDefinitionUI(List<Integer> clarityList, int crrentClarity) {
        mSuper.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mHighclear.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mCommon.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        mFluency.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
        if (clarityList.contains(SohuPlayerDefinition.PE_DEFINITION_ORIGINAL)) {
            mSuper.setVisibility(View.VISIBLE);
            mSuper.setClickable(true);
        } else {
            mSuper.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mSuper.setClickable(false);
        }
        if (clarityList.contains(SohuPlayerDefinition.PE_DEFINITION_SUPER)) {
            mHighclear.setVisibility(View.VISIBLE);
            mHighclear.setClickable(true);
        } else {
            mHighclear.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mHighclear.setClickable(false);
        }
        if (clarityList.contains(SohuPlayerDefinition.PE_DEFINITION_HIGH)) {
            mCommon.setVisibility(View.VISIBLE);
            mCommon.setClickable(true);
        } else {
            mCommon.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mCommon.setClickable(false);
        }
        if (clarityList.contains(SohuPlayerDefinition.PE_DEFINITION_NOR)) {
            mFluency.setVisibility(View.VISIBLE);
            mFluency.setClickable(true);
        } else {
            mFluency.setTextColor(context.getResources().getColor(R.color.color_7e7e7e));
            mFluency.setClickable(false);
        }

        if (SohuPlayerDefinition.PE_DEFINITION_ORIGINAL == crrentClarity) {
            mSuper.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (SohuPlayerDefinition.PE_DEFINITION_SUPER == crrentClarity) {
            mHighclear.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (SohuPlayerDefinition.PE_DEFINITION_HIGH == crrentClarity) {
            mCommon.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        } else if (SohuPlayerDefinition.PE_DEFINITION_NOR == crrentClarity) {
            mFluency.setTextColor(context.getResources().getColor(R.color.color_3599f8));
        }
    }

    public void showPopView(Map<String, Boolean> claritymap, String lvideoVideoCode) {
        initClarityUI(claritymap, lvideoVideoCode);
        try {
            Looper.myQueue().addIdleHandler(new IdleHandler() {
                @Override
                public boolean queueIdle() {
                    try {
                        int[] location = new int[2];
                        mparentView.getLocationOnScreen(location);
                        mSiftPopupWindow.showAtLocation(mparentView, Gravity.RIGHT, 0, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return false;
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void disMissPopView() {
        if (mSiftPopupWindow != null && mSiftPopupWindow.isShowing()) {
            mSiftPopupWindow.dismiss();
        }
    }

    /**
     * 控制清晰度UI
     */
    private void initClarityUI(Map<String, Boolean> claritymap, String lvideoVideoCode) {
        if (null != claritymap) {
            //超清, PLS_MP4_720P = "252051",PLS_FLV_720P = "252018",PLS_FLV_720P_3D = "252029"
            if ((null != claritymap.get(DefinitionManager.PLS_MP4_720P)
                    && claritymap.get(DefinitionManager.PLS_MP4_720P))
                    || (null != claritymap.get(DefinitionManager.PLS_FLV_720P)
                    && claritymap.get(DefinitionManager.PLS_FLV_720P))
                    || (null != claritymap.get(DefinitionManager.PLS_FLV_720P_3D)
                    && claritymap.get(DefinitionManager.PLS_FLV_720P_3D))) {
                if (DefinitionManager.PLS_MP4_720p_db.equals(lvideoVideoCode)) {
//                    mSuper.setTextColor(context.getResources().getColor(R.color.color_3599f8));//当前清晰度
                    mSuper.setEnabled(true);
                    mSuper.setSelected(true);
                } else {
                    mSuper.setEnabled(true);
                    mSuper.setSelected(false);
//                    mSuper.setTextColor(context.getResources().getColor(R.color.white));//正常状态
                }
            } else {
                mSuper.setEnabled(false);
                mSuper.setSelected(false);
//                mSuper.setTextColor(context.getResources().getColor(R.color.gray_cccccc));//不可用
            }

            //高清，PLS_MP4_720p_db = "252022"
            if (null != claritymap.get(DefinitionManager.PLS_MP4_720p_db)
                    && claritymap.get(DefinitionManager.PLS_MP4_720p_db)) {
                if (DefinitionManager.PLS_MP4_720p_db.equals(lvideoVideoCode)) {
                    mHighclear.setEnabled(true);
                    mHighclear.setSelected(true);
//                    mHighclear.setClickable(false);
//                    mHighclear.setTextColor(context.getResources().getColor(R.color.color_3599f8));//当前清晰度
                } else {
                    mHighclear.setSelected(false);
                    mHighclear.setEnabled(true);
//                    mHighclear.setTextColor(context.getResources().getColor(R.color.white));//正常状态
                }
            } else {
                mHighclear.setEnabled(false);
                mHighclear.setSelected(false);
//                mHighclear.setClickable(false);
//                mHighclear.setTextColor(context.getResources().getColor(R.color.gray_cccccc));//不可用
            }

            //流畅，PLS_MP4 = "252009"，PLS_MP4_350 = "252021"
            if ((null != claritymap.get(DefinitionManager.PLS_MP4_350)
                    && claritymap.get(DefinitionManager.PLS_MP4_350))
                    || (null != claritymap.get(DefinitionManager.PLS_MP4)
                    && claritymap.get(DefinitionManager.PLS_MP4))) {
                if (DefinitionManager.PLS_MP4_350.equals(lvideoVideoCode)
                        || DefinitionManager.PLS_MP4.equals(lvideoVideoCode)) {
                    mFluency.setEnabled(true);
                    mHighclear.setSelected(true);
//                    mFluency.setTextColor(context.getResources().getColor(R.color.color_3599f8));
                } else {
                    mFluency.setEnabled(true);
                    mHighclear.setSelected(false);
//                    mFluency.setTextColor(context.getResources().getColor(R.color.white));
                }
            } else {
                mFluency.setEnabled(false);
                mHighclear.setSelected(false);
//                mFluency.setClickable(false);
//                mFluency.setTextColor(context.getResources().getColor(R.color.gray_cccccc));
            }

            //标清，PLS_MP4_800 = "252013"
            if (null != claritymap.get(DefinitionManager.PLS_MP4_800)
                    && claritymap.get(DefinitionManager.PLS_MP4_800)) {
                if (DefinitionManager.PLS_MP4_800.equals(lvideoVideoCode)) {
                    mCommon.setEnabled(false);
                    mCommon.setSelected(true);
//                    mCommon.setSelected(true);
//                    mCommon.setTextColor(context.getResources().getColor(R.color.color_3599f8));
                } else {
                    mCommon.setEnabled(true);
                    mCommon.setSelected(false);
//                    mCommon.setTextColor(context.getResources().getColor(R.color.white));
                }
            } else {
                mCommon.setEnabled(false);
                mCommon.setSelected(false);
//                mCommon.setClickable(false);
//                mCommon.setTextColor(context.getResources().getColor(R.color.gray_cccccc));
            }
        }
    }

    private void initView() {
        mSuper = (TextView) mPopWindowView.findViewById(R.id.definition_super);
        mHighclear = (TextView) mPopWindowView.findViewById(R.id.definition_highclear);
        mCommon = (TextView) mPopWindowView.findViewById(R.id.definition_common);
        mFluency = (TextView) mPopWindowView.findViewById(R.id.definition_fluency);
        mSuper.setOnClickListener(mClickListener);
        mHighclear.setOnClickListener(mClickListener);
        mCommon.setOnClickListener(mClickListener);
        mFluency.setOnClickListener(mClickListener);
    }

    public PopupWindow getmSiftPopupWindow() {
        return mSiftPopupWindow;
    }


    public void showPopView(Map<String, Boolean> claritymap, String lvideoVideoCode
            , String playerType) {
//        if (TextUtils.equals(siteType,))
        initCloudClarityUI(claritymap, lvideoVideoCode);
        try {
            Looper.myQueue().addIdleHandler(new IdleHandler() {
                @Override
                public boolean queueIdle() {
                    try {
                        int[] location = new int[2];
                        mparentView.getLocationOnScreen(location);
                        mSiftPopupWindow.showAtLocation(mparentView, Gravity.RIGHT, 0, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return false;
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 控制清晰度UI--云盘源
     */
    public void initCloudClarityUI(Map<String, Boolean> supportDefFlag, String currentClarity) {
        if (null != supportDefFlag) {
            DefinitionManager.DefinitionObj definitionObj
                    = DefinitionManager.checkDefClassify(currentClarity);
            //超清
            if (supportDefFlag.containsKey(DefinitionManager.DEFINITION_TYPE_SUPER)
                    && supportDefFlag.get(DefinitionManager.DEFINITION_TYPE_SUPER)) {
                if (definitionObj != null
                        && (TextUtils.equals(DefinitionManager.DEFINITION_TYPE_SUPER
                        , definitionObj.defType))) {
                    mSuper.setTextColor(context.getResources().getColor(R.color.color_3599f8));//当前清晰度
                    mSuper.setEnabled(true);
                    mSuper.setSelected(true);
                } else {
                    mSuper.setEnabled(true);
                    mSuper.setSelected(false);
                    mSuper.setTextColor(context.getResources().getColor(R.color.white));//正常状态
                }
            } else {
                mSuper.setEnabled(false);
                mSuper.setSelected(false);
                mSuper.setTextColor(context.getResources().getColor(R.color.text_222222));//不可用
            }

            //高清
            if (supportDefFlag.containsKey(DefinitionManager.DEFINITION_TYPE_HIGH)
                    && supportDefFlag.get(DefinitionManager.DEFINITION_TYPE_HIGH)) {
                if (definitionObj != null
                        && (TextUtils.equals(DefinitionManager.DEFINITION_TYPE_HIGH
                        , definitionObj.defType))) {
                    mHighclear.setEnabled(true);
                    mHighclear.setSelected(true);
//                    mHighclear.setClickable(false);
                    mHighclear.setTextColor(context.getResources().getColor(R.color.color_3599f8));//当前清晰度
                } else {
                    mHighclear.setSelected(false);
                    mHighclear.setEnabled(true);
                    mHighclear.setTextColor(context.getResources().getColor(R.color.white));//正常状态
                }
            } else {
                mHighclear.setEnabled(false);
                mHighclear.setSelected(false);
//                mHighclear.setClickable(false);
                mHighclear.setTextColor(context.getResources().getColor(R.color.text_222222));//不可用
            }

            //流畅
            if (supportDefFlag.containsKey(DefinitionManager.DEFINITION_TYPE_FLUENCY)
                    && supportDefFlag.get(DefinitionManager.DEFINITION_TYPE_FLUENCY)) {
                if (definitionObj != null
                        && (TextUtils.equals(DefinitionManager.DEFINITION_TYPE_FLUENCY
                        , definitionObj.defType))) {
                    mFluency.setEnabled(true);
                    mFluency.setSelected(true);
                    mFluency.setTextColor(context.getResources().getColor(R.color.color_3599f8));
                } else {
                    mFluency.setEnabled(true);
                    mFluency.setSelected(false);
                    mFluency.setTextColor(context.getResources().getColor(R.color.white));
                }
            } else {
                mFluency.setEnabled(false);
                mFluency.setSelected(false);
//                mFluency.setClickable(false);
                mFluency.setTextColor(context.getResources().getColor(R.color.text_222222));
            }

            //标清
            if (supportDefFlag.containsKey(DefinitionManager.DEFINITION_TYPE_NORMAL)
                    && supportDefFlag.get(DefinitionManager.DEFINITION_TYPE_NORMAL)) {
                if (definitionObj != null
                        && (TextUtils.equals(DefinitionManager.DEFINITION_TYPE_NORMAL
                        , definitionObj.defType))) {
                    mCommon.setEnabled(true);
                    mCommon.setSelected(true);
//                    mCommon.setSelected(true);
                    mCommon.setTextColor(context.getResources().getColor(R.color.color_3599f8));
                } else {
                    mCommon.setEnabled(true);
                    mCommon.setSelected(false);
                    mCommon.setTextColor(context.getResources().getColor(R.color.white));
                }
            } else {
                mCommon.setEnabled(false);
                mCommon.setSelected(false);
//                mCommon.setClickable(false);
                mCommon.setTextColor(context.getResources().getColor(R.color.text_222222));
            }
        }
    }
}
